package com.shou.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shou.BusinessException;
import com.shou.CommonVar;
import com.shou.controller.MyController;
import com.shou.dao.CollectionDao;
import com.shou.dao.ServeInfoDao;
import com.shou.dao.ServeTypeDao;
import com.shou.dao.UserDao;
import com.shou.entity.Collection;
import com.shou.entity.ServeInfo;
import com.shou.entity.ServeType;
import com.shou.entity.User;
import com.shou.entity.weixinbean.AccessToken;
import com.shou.entity.weixinbean.WeixinUser;
import com.shou.task.WeixinTask;
import com.shou.util.WeixinUtil;

@Service("userService")
public class UserService extends BaseService{

	@Autowired
	private CollectionDao collectionDaoImpl;
	
	@Autowired
	private ServeInfoDao serveInfoDaoImpl;
	
	@Autowired
	private ServeTypeDao serveTypeDaoImpl;
	
	@Autowired
	private UserDao userDaoImpl;
	
	public void login(HttpServletRequest request,String code) throws IOException {
		
		String WEIXIN_APPID = getConfig("WEIXIN_APPID");
		String WEIXIN_APPSECRET = getConfig("WEIXIN_APPSECRET");
		
		//AccessToken token = WeixinUtil.getAccessToken(WEIXIN_APPID, WEIXIN_APPSECRET);
		AccessToken token = WeixinTask.getAccessToken();
		String openId = WeixinUtil.getOpenIdByCode(code, WEIXIN_APPID,WEIXIN_APPSECRET);
		WeixinUser weixinUser = WeixinUtil.getUserInfo(openId,token.getToken());
		
		User user = userDaoImpl.getBy(User.class, "openid",openId);
		if(user == null){				 
			user = new User();
			user.setNickname(weixinUser.getNickname());
			user.setHeadimgurl(weixinUser.getHeadimgurl());
			user.setCountry(weixinUser.getCountry());
			user.setCity(weixinUser.getCity());
			user.setOpenid(openId);
			user.setProvince(weixinUser.getProvince());
			user.setSex(weixinUser.getSex());
			user.setLanguage(weixinUser.getLanguage());
			user = userDaoImpl.saveOrUpdate(user);
		}			
		request.getSession().setAttribute(CommonVar.SESSION_WEIXIN,user);
	}
	
	public User getUserInfo(Class<User> clazz, String propertyName,String value) {
		return userDaoImpl.getBy(clazz, propertyName, value);
	}
	
	public User saveUser(User user){
		return userDaoImpl.saveOrUpdate(user);
	}
	
	public void collectServe(Collection collection) {
		collection.setUseStatus(CommonVar.USE_ALL);
		List<Collection> colList = collectionDaoImpl.queryList(collection);
		if(colList.size() > 0) {
			Collection col = colList.get(0);
			if(col.getUseStatus().intValue() == CommonVar.USE_ONUSE){
				//用户已收藏
				throw new BusinessException("您已经收藏过");
			}
			else {
				col.setUseStatus(CommonVar.USE_ONUSE);
				col.setUpdateTime(new Date());
				collectionDaoImpl.saveOrUpdate(col);
				return;
			}
		}
		else {
			collection.setUseStatus(CommonVar.USE_ONUSE);
			collection.setCreateTime(new Date());
			collection.setUpdateTime(new Date());
			collectionDaoImpl.saveOrUpdate(collection);
		}		
	}
	
	public void cancelCollection(Long userId,Long serveId) {
		Collection colQuery = new Collection();
		List<Collection> list = collectionDaoImpl.queryList(colQuery);
		if(list.size() <= 0) {
			throw new BusinessException("对不起，您的操作有误");
		}
		else if(list.get(0) == null || list.get(0).getUseStatus() == CommonVar.USE_NOUSE){
			throw new BusinessException("对不起，您的操作有误");
		}
		else {
			Collection collection = list.get(0);
			collection.setUseStatus(CommonVar.USE_NOUSE);
			collection.setUpdateTime(new Date());
			collectionDaoImpl.saveOrUpdate(collection);
		}
	}
	
	/**
	 * 查询用户的收藏记录
	 * @param userId
	 * @return
	 */
	public List<ServeInfo> getMyCollectServe(Long userId) {
		List<ServeInfo> list = serveInfoDaoImpl.getUserCollectServe(userId);
		for(ServeInfo info:list) {
			ServeType type = serveTypeDaoImpl.get(ServeType.class,info.getServeTypeId());
			info.setServeTypeName(type.getTypeName());
			info.setServeTypeColor(type.getTypeColor());
		}
		return list;
	}
	
	/**
	 * 查询用户的发布记录
	 * @param userId
	 * @return
	 */
	public List<ServeInfo> getMyPushInfo(Long userId) {
		ServeInfo serveQuery = new ServeInfo();
		serveQuery.setUseStatus(CommonVar.USE_ONUSE);
		serveQuery.setUserId(userId);
		List<ServeInfo> list = serveInfoDaoImpl.queryList(serveQuery);
		for(ServeInfo info:list) {
			ServeType type = serveTypeDaoImpl.get(ServeType.class,info.getServeTypeId());
			info.setServeTypeName(type.getTypeName());
			info.setServeTypeColor(type.getTypeColor());			
		}
		return serveInfoDaoImpl.queryList(serveQuery);
	}
}
